home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Networking / MacTCP / MPing 1.1 / Sources / MPing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-30  |  37.1 KB  |  1,185 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #    MPing 1.1 - MacTCP Ping Tool
  3. #
  4. #    Copyright © Apple Computer, Inc. 1990-1991
  5. #    All rights reserved.
  6. #
  7. #    Versions:    
  8. #                1.1        September 25, 1991.
  9. #
  10. #    File:
  11. #                MPing.c
  12. #
  13. #    Components:
  14. #                AddressXlation.h
  15. #                MacTCPCommonTypes.h
  16. #                Makefile
  17. #                MiscIPPB.h
  18. #                MPing.c
  19. #                MPing.h
  20. #                MPing.r
  21. #                MPingDlg.c
  22. #                MPingExtern.h
  23. #                MPingGlobals.h
  24. #                MPingIcmp.c
  25. #                MPingWindow.c
  26. #                resolver.c
  27. ------------------------------------------------------------------------------*/
  28.  
  29. #include <Values.h>
  30. #include <Types.h>
  31. #include <Resources.h>
  32. #include <QuickDraw.h>
  33. #include <Fonts.h>
  34. #include <Events.h>
  35. #include <Windows.h>
  36. #include <Menus.h>
  37. #include <TextEdit.h>
  38. #include <Dialogs.h>
  39. #include <Desk.h>
  40. #include <ToolUtils.h>
  41. #include <Memory.h>
  42. #include <SegLoad.h>
  43. #include <Files.h>
  44. #include <OSUtils.h>
  45. #include <OSEvents.h>
  46. #include <DiskInit.h>
  47. #include <Packages.h>
  48. #include <Traps.h>
  49. #include <String.h>
  50. #include <StdLib.h>
  51. #include <StdIO.h>
  52. #include <CursorCtl.h>
  53. #include <Sound.h>
  54.  
  55. #include "MacTCPCommonTypes.h"
  56. #include "MPing.h"                /* bring in all the #defines for MPing */
  57. #include "AddressXlation.h"
  58. #include "MPingGlobals.h"        /* bring in some structure definitions */
  59. #include "MPingExtern.h"        /* all extern variables */
  60.  
  61. SysEnvRec    gMac;                /* set up by Initialize */
  62.  
  63. Boolean        gHasWaitNextEvent;    /* set up by Initialize */
  64.  
  65. Boolean        gInBackground;        /* maintained by Initialize and DoEvent */
  66.  
  67. Boolean        gRunning=false;        /* maintained by Initialize() and ICMP */
  68.  
  69. Rect        gStopRect;            /* set up by Initialize */
  70. Rect        gGoRect;            /* set up by Initialize() */
  71. Rect        gMsgRect;            /* set up by initialize() - ICMP messages */
  72.  
  73. short        gFlipCursor=128;    /* used as a cursor animation flag */
  74. short        gCursorCount=0;        /* animate the cursor during ping */
  75.  
  76. unsigned long    gTickCount;        /* for timing */
  77.  
  78. extern long    gTempCount;
  79. extern Boolean gFixedNoOfPkts;
  80.  
  81. void EventLoop( void );
  82. void DoEvent( EventRecord *event );
  83. void AdjustCursor( Point mouse, RgnHandle region );
  84. void GetGlobalMouse( Point *mouse );
  85. void DoUpdate( WindowPtr window );
  86. void DoActivate( WindowPtr window, Boolean becomingActive );
  87. void DoContentClick( WindowPtr window, EventRecord *event );
  88. void DrawWindow( WindowPtr window );
  89. void AdjustMenus( void );
  90. void DoMenuCommand( long menuResult );
  91. Boolean DoCloseWindow( WindowPtr window );
  92. void Terminate( void );
  93. void Initialize( void );
  94. Boolean GoGetRect( short rectID, Rect *theRect );
  95. void ForceEnvirons( void );
  96. Boolean IsAppWindow( WindowPtr window );
  97. Boolean IsDAWindow( WindowPtr window );
  98. Boolean TrapAvailable( short tNumber, TrapType tType );
  99. void AlertUser( short error );
  100. void WarnUser( short error );
  101. void DrawPingStatistics(WindowPtr window);
  102. void DrawOnScreen(void);
  103. void DrawPacketOnScreen(void);
  104. void DrawErrorMessageOnScreen(short errMsg);
  105. void DrawHostInfoOnScreen(void);
  106. void DoKeyDown( EventRecord *event );
  107. void DoSendIcmpPackets(void);
  108.  
  109. extern void DoDialogItem(DialogPtr ldialog, short itemHit);
  110. extern DoInitDialog(DialogPtr ldialog);
  111. extern OSErr OpenResolver(char *fileName);
  112. extern OSErr CloseResolver(void);
  113. extern void AdjustHV(Boolean isVert, ControlHandle control, TEHandle docTE, Boolean canRedraw);
  114. extern void AdjustScrollValues(WindowPtr window, Boolean canRedraw);
  115. extern pascal void VActionProc(ControlHandle control, short part);
  116. extern void AdjustScrollbars(WindowPtr window, Boolean needsResize);
  117. extern void AdjustScrollSizes(WindowPtr window);
  118. extern void GetTERect(WindowPtr window, Rect *teRect);
  119. extern void AdjustViewRect(TEHandle docTE);
  120. extern void AdjustTE(WindowPtr window);
  121. extern void mycopy(char *dest, char *src, short noOfBytes);
  122. extern void SendIcmpEcho(void);
  123. extern void DoPingStop(DialogPtr ldialog, Handle itHandle);
  124.  
  125. /* Define HiWrd and LoWrd macros for efficiency. */
  126. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  127. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  128.  
  129. /* Define TopLeft and BotRight macros for convenience. Notice the implicit
  130.    dependency on the ordering of fields within a Rect */
  131. #define TopLeft(aRect)    (* (Point *) &(aRect).top)
  132. #define BotRight(aRect)    (* (Point *) &(aRect).bottom)
  133.  
  134.  
  135. extern void _DataInit();
  136.  
  137. /* This routine is part of the MPW runtime library. This external
  138.    reference to it is done so that we can unload its segment, %A5Init. */
  139.  
  140.  
  141. #pragma segment Main
  142. main()
  143. {
  144.     UnloadSeg((Ptr) _DataInit);        /* note that _DataInit must not be in Main! */
  145.     
  146.     /* 1.01 - call to ForceEnvirons removed */
  147.     
  148.     /*    If you have stack requirements that differ from the default,
  149.         then you could use SetApplLimit to increase StackSpace at 
  150.         this point, before calling MaxApplZone. */
  151.     MaxApplZone();                    /* expand the heap so code segments load at the top */
  152.  
  153.     Initialize();                    /* initialize the program */
  154.     UnloadSeg((Ptr) Initialize);    /* note that Initialize must not be in Main! */
  155.     EventLoop();                    /* call the main event loop */
  156. }
  157.  
  158.  
  159. /*    Get events forever, and handle them by calling DoEvent.
  160.     Get the events by calling WaitNextEvent, if it's available, otherwise
  161.     by calling GetNextEvent. Also call AdjustCursor each time through the loop. */
  162.  
  163. #pragma segment Main
  164. void EventLoop()
  165. {
  166.     RgnHandle    cursorRgn;
  167.     Boolean        gotEvent;
  168.     EventRecord    event;
  169.     Point        mouse;
  170.     WindowPtr    window;
  171.     DialogPtr    ldialog;
  172.     short        itemHit;
  173.     short        titType;
  174.     Handle        titHandle;
  175.     Rect        titDispRect;
  176.  
  177.     cursorRgn = NewRgn();            /* we’ll pass WNE an empty region the 1st time thru */
  178.     do {
  179.         /* use WNE if it is available */
  180.         if ( gHasWaitNextEvent ) {
  181.             GetGlobalMouse(&mouse);
  182.             AdjustCursor(mouse, cursorRgn);
  183.             gotEvent = WaitNextEvent(everyEvent, &event, MAXLONG, cursorRgn);
  184.         }
  185.         else {
  186.             SystemTask();
  187.             gotEvent = GetNextEvent(everyEvent, &event);
  188.         }
  189.         if ( gotEvent ) {
  190.             /* make sure we have the right cursor before handling the event */
  191.             AdjustCursor(event.where, cursorRgn);
  192.             DoEvent(&event);
  193.         }
  194.         if (window = FrontWindow()) {
  195.             if (IsDialogEvent(&event)) {
  196.                 if (DialogSelect(&event, &ldialog, &itemHit))
  197.                     DoDialogItem(ldialog, itemHit);
  198.             }
  199.             if ((gSessionComplete == true) && (gRunning == true)) {
  200.                 GetDItem(window, PINGSTOP_BUTTON, &titType, &titHandle, &titDispRect);
  201.                 DoPingStop((DialogPtr) window, titHandle);
  202.             }
  203.         }
  204.         DoSendIcmpPackets();        /* send icmp echo request to the destination host */
  205.     } while ( true );    /* loop forever; we quit via ExitToShell */
  206. } /*EventLoop*/
  207.  
  208.  
  209. void DoSendIcmpPackets(void)
  210. {
  211.     unsigned long myTickCount;
  212.  
  213.     if ((gQuietVerbose == false) && (gResponsePrinted == false) && (gRunning == true)) {
  214.         DrawPacketOnScreen();        /* verbose mode, draw packet information */
  215.         gResponsePrinted = true;
  216.     }
  217.     if ((gRunning == true) && (gSessionComplete == false)) {    /* make sure we still want to send icmp echoes */
  218.         myTickCount = TickCount();
  219.         if (myTickCount >= (gTickCount+gWait)) {
  220.             gTickCount = myTickCount;
  221.             SendIcmpEcho();
  222.         }
  223.     }
  224. }
  225.  
  226. /* Do the right thing for an event. Determine what kind of event it is, and call
  227.  the appropriate routines. */
  228.  
  229. #pragma segment Main
  230. void DoEvent(event)
  231.     EventRecord    *event;
  232. {
  233.     short        part, err;
  234.     WindowPtr    window;
  235.     Boolean        hit;
  236.     char        key;
  237.     Point        aPoint;
  238.  
  239.     switch ( event->what ) {
  240.         case mouseDown:
  241.             part = FindWindow(event->where, &window);
  242.             switch ( part ) {
  243.                 case inMenuBar:                /* process a mouse menu command (if any) */
  244.                     AdjustMenus();
  245.                     DoMenuCommand(MenuSelect(event->where));
  246.                     break;
  247.                 case inSysWindow:            /* let the system handle the mouseDown */
  248.                     SystemClick(event, window);
  249.                     break;
  250.                 case inContent:
  251.                     if ( window != FrontWindow() ) {
  252.                         SelectWindow(window);
  253.                         /*DoEvent(event);*/    /* use this line for "do first click" */
  254.                     } else
  255.                         DoContentClick(window, event);
  256.                     break;
  257.                 case inDrag:                /* pass screenBits.bounds to get all gDevices */
  258.                     DragWindow(window, event->where, &qd.screenBits.bounds);
  259.                     break;
  260.                 case inGrow:
  261.                     break;
  262.                 case inZoomIn:
  263.                 case inZoomOut:
  264.                     hit = TrackBox(window, event->where, part);
  265.                     if ( hit ) {
  266.                         SetPort(window);                /* the window must be the current port... */
  267.                         EraseRect(&window->portRect);    /* because of a bug in ZoomWindow */
  268.                         ZoomWindow(window, part, true);    /* note that we invalidate and erase... */
  269.                         InvalRect(&window->portRect);    /* to make things look better on-screen */
  270.                     }
  271.                     break;
  272.             }
  273.             break;
  274.         case keyDown:
  275.         case autoKey:                        /* check for menukey equivalents */
  276.             key = event->message & charCodeMask;
  277.             if ( event->modifiers & cmdKey ) {            /* Command key down */
  278.                 if ( event->what == keyDown ) {
  279.                     AdjustMenus();                        /* enable/disable/check menu items properly */
  280.                     DoMenuCommand(MenuKey(key));
  281.                 }
  282.             }
  283.             else ;
  284.             break;
  285.         case activateEvt:
  286.             DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
  287.             break;
  288.         case updateEvt:
  289.             DoUpdate((WindowPtr) event->message);
  290.             break;
  291.         /*    1.01 - It is not a bad idea to at least call DIBadMount in response
  292.             to a diskEvt, so that the user can format a floppy. */
  293.         case diskEvt:
  294.             if ( HiWord(event->message) != noErr ) {
  295.                 SetPt(&aPoint, kDILeft, kDITop);
  296.                 err = DIBadMount(aPoint, event->message);
  297.             }
  298.             break;
  299.         case kOSEvent:
  300.         /*    1.02 - must BitAND with 0x0FF to get only low byte */
  301.             switch ((event->message >> 24) & 0x0FF) {        /* high byte of message */
  302.                 case kSuspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  303.                     gInBackground = (event->message & kResumeMask) == 0;
  304.                     DoActivate(FrontWindow(), !gInBackground);
  305.                     break;
  306.             }
  307.             break;
  308.     }
  309. } /*DoEvent*/
  310.  
  311.  
  312. /*    Change the cursor's shape, depending on its position. This also calculates the region
  313.     where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  314.     that region, an event would be generated, causing this routine to be called,
  315.     allowing us to change the region to the region the mouse is currently in. If
  316.     there is more to the event than just “the mouse moved”, we get called before the
  317.     event is processed to make sure the cursor is the right one. In any (ahem) event,
  318.     this is called again before we     fall back into WNE. */
  319.  
  320. #pragma segment Main
  321. void AdjustCursor(mouse,region)
  322.     Point        mouse;
  323.     RgnHandle    region;
  324. {
  325.     WindowPtr    window;
  326.     RgnHandle    arrowRgn;
  327.     RgnHandle    curAnimRgn;
  328.     Rect        curAnimRect;
  329.  
  330.     window = FrontWindow();    /* we only adjust the cursor when we are in front */
  331.     if ( (! gInBackground) && (! IsDAWindow(window)) ) {
  332.         /* calculate regions for different cursor shapes */
  333.         arrowRgn = NewRgn();
  334.         curAnimRgn = NewRgn();
  335.  
  336.         /* start with a big, big rectangular region */
  337.         SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
  338.  
  339.         /* calculate curAnimRgn */
  340.         if ( IsAppWindow(window) ) {
  341.             curAnimRect = (*((DocumentPeek) window)->docTE)->viewRect;
  342.             SetPort(window);    /* make a global version of the viewRect */
  343.             LocalToGlobal(&TopLeft(curAnimRect));
  344.             LocalToGlobal(&BotRight(curAnimRect));
  345.             RectRgn(curAnimRgn, &curAnimRect);
  346.             SetOrigin(-window->portBits.bounds.left, -window->portBits.bounds.top);
  347.             SectRgn(curAnimRgn, window->visRgn, curAnimRgn);
  348.             SetOrigin(0, 0);
  349.         }
  350.  
  351.         /* subtract other regions from arrowRgn */
  352.         DiffRgn(arrowRgn, curAnimRgn, arrowRgn);
  353.  
  354.         /* change the cursor and the region parameter */
  355.         if ( PtInRgn(mouse, curAnimRgn) && (gRunning == true) ) {
  356.             gFlipCursor = (gFlipCursor == gLastCursor ? gFirstCuror : gFlipCursor+1);
  357.             SetCursor(*GetCursor(gFlipCursor));
  358.             SpinCursor(1);        /* spin cursor by one frame */
  359.             CopyRgn(curAnimRgn, region);
  360.         } else {
  361.             SetCursor(&qd.arrow);
  362.             CopyRgn(arrowRgn, region);
  363.         }
  364.  
  365.         /* get rid of our local regions */
  366.         DisposeRgn(arrowRgn);
  367.         DisposeRgn(curAnimRgn);
  368.     }
  369. } /*AdjustCursor*/
  370.  
  371.  
  372. /*    Get the global coordinates of the mouse. When you call OSEventAvail
  373.     it will return either a pending event or a null event. In either case,
  374.     the where field of the event record will contain the current position
  375.     of the mouse in global coordinates and the modifiers field will reflect
  376.     the current state of the modifiers. Another way to get the global
  377.     coordinates is to call GetMouse and LocalToGlobal, but that requires
  378.     being sure that thePort is set to a valid port. */
  379.  
  380. #pragma segment Main
  381. void GetGlobalMouse(mouse)
  382.     Point    *mouse;
  383. {
  384.     EventRecord    event;
  385.     
  386.     OSEventAvail(kNoEvents, &event);    /* we aren't interested in any events */
  387.     *mouse = event.where;                /* just the mouse position */
  388. } /*GetGlobalMouse*/
  389.  
  390.  
  391. /*    This is called when an update event is received for a window.
  392.     It calls DrawWindow to draw the contents of an application window.
  393.     As an effeciency measure that does not have to be followed, it
  394.     calls the drawing routine only if the visRgn is non-empty. This
  395.     will handle situations where calculations for drawing or drawing
  396.     itself is very time-consuming. */
  397.  
  398. #pragma segment Main
  399. void DoUpdate(window)
  400.     WindowPtr    window;
  401. {
  402.     if ( IsAppWindow(window) ) {
  403.         BeginUpdate(window);                /* this sets up the visRgn */
  404.         if ( ! EmptyRgn(window->visRgn) ) {    /* draw if updating needs to be done */
  405.             UpdtDialog((DialogPtr) window, window->visRgn);
  406.             TEUpdate(&(window->portRect), ((DocumentPeek) window)->docTE);
  407.             FrameRect(&gMsgRect);
  408.         }
  409.         EndUpdate(window);
  410.     }
  411. } /*DoUpdate*/
  412.  
  413.  
  414. /*    This is called when a window is activated or deactivated.
  415.     In Sample, the Window Manager's handling of activate and
  416.     deactivate events is sufficient. Other applications may have
  417.     TextEdit records, controls, lists, etc., to activate/deactivate. */
  418.  
  419. #pragma segment Main
  420. void DoActivate(window, becomingActive)
  421.     WindowPtr    window;
  422.     Boolean        becomingActive;
  423. {
  424.     Rect        growRect;
  425.  
  426.     if ( IsAppWindow(window) ) {
  427.         if ( becomingActive ) {
  428.             /* do whatever you need to at activation */
  429.             /* the growbox needs to be redrawn on activation: */
  430.             /* adjust for the scrollbars */
  431.             GetTERect( window, &growRect);
  432.             InvalRect(&growRect);
  433.             ShowControl( ((DocumentPeek) window)->docVScroll );
  434.             DrawControls(window);
  435.  
  436.         }
  437.         else {
  438.             /* do whatever you need to at deactivation */
  439.             HideControl( ((DocumentPeek) window)->docVScroll );
  440.         }
  441.     }
  442. } /*DoActivate*/
  443.  
  444.  
  445. /*    This is called when a mouse-down event occurs in the content of a window.
  446.     Other applications might want to call FindControl, TEClick, etc., to
  447.     further process the click. */
  448.  
  449. #pragma segment Main
  450. void DoContentClick(window, event)
  451.     WindowPtr    window;
  452.     EventRecord *event;
  453. {
  454.     Point        mouse;
  455.     ControlHandle control;
  456.     short        part, value;
  457.     Boolean        shiftDown;
  458.     DocumentPeek doc;
  459.     Rect        teRect;
  460.  
  461.     if ( IsAppWindow(window) ) {
  462.         SetPort(window);
  463.         mouse = event->where;                /* get the click position */
  464.         GlobalToLocal(&mouse);
  465.         doc = (DocumentPeek) window;
  466.         GetTERect(window, &teRect);
  467.         if ( PtInRect(mouse, &teRect) ) {
  468.             /* see if we need to extend the selection */
  469.             shiftDown = (event->modifiers & shiftKey) != 0;    /* extend if Shift is down */
  470.         } else {
  471.             part = FindControl(mouse, window, &control);
  472.             switch ( part ) {
  473.                 case 0:                            /* do nothing for viewRect case */
  474.                     break;
  475.                 case inThumb:
  476.                     value = GetCtlValue(control);
  477.                     part = TrackControl(control, mouse, nil);
  478.                     if ( part != 0 ) {
  479.                         value -= GetCtlValue(control);
  480.                         /* value now has CHANGE in value; if value changed, scroll */
  481.                         if ( value != 0 )
  482.                             if ( control == doc->docVScroll )
  483.                                 TEScroll(0, value * (*doc->docTE)->lineHeight, doc->docTE);
  484.                     }
  485.                     break;
  486.                 default:        /* they clicked in an arrow, so track & scroll */
  487.                     if ( control == doc->docVScroll )
  488.                         value = TrackControl(control, mouse, (ProcPtr) VActionProc);
  489.                     break;
  490.             }
  491.         }
  492.     }
  493.  
  494. } /*DoContentClick*/
  495.  
  496.  
  497. /* This is called for any keyDown or autoKey events, except when the
  498.  Command key is held down. It looks at the frontmost window to decide what
  499.  to do with the key typed. */
  500.  
  501. #pragma segment Main
  502. void DoKeyDown(event)
  503.     EventRecord    *event;
  504. {
  505.     WindowPtr    window;
  506.     char        key;
  507.     TEHandle    te;
  508.  
  509.     window = FrontWindow();
  510.     if ( IsAppWindow(window) ) {
  511.         te = ((DocumentPeek) window)->docTE;
  512.         key = event->message & charCodeMask;
  513.         /* we have a char. for our window; see if we are still below TextEdit’s
  514.             limit for the number of characters (but deletes are always rad) */
  515.         if ( key == kDelChar ||
  516.                 (*te)->teLength - ((*te)->selEnd - (*te)->selStart) + 1 <
  517.                 kMaxTELength ) {
  518.             TEKey(key, te);
  519.             AdjustScrollbars(window, false);
  520.             AdjustTE(window);
  521.         } else
  522.             AlertUser(sExceedChar);
  523.     }
  524. } /*DoKeyDown*/
  525.  
  526.  
  527. /* Draw the contents of the application window. We do some drawing in color, using
  528.    Classic QuickDraw's color capabilities. This will be black and white on old
  529.    machines, but color on color machines. At this point, the window’s visRgn
  530.    is set to allow drawing only where it needs to be done. */
  531.  
  532. #pragma segment Main
  533. void DrawWindow(window)
  534.     WindowPtr    window;
  535. {
  536.     SetPort(window);
  537.  
  538.     EraseRect(&window->portRect);    /* clear out any garbage that may linger */
  539.     DrawDialog((DialogPtr) window);
  540.     DrawPingStatistics(window);
  541. } /*DrawWindow*/
  542.  
  543.  
  544. /*    Enable and disable menus based on the current state.
  545.     The user can only select enabled menu items. We set up all the menu items
  546.     before calling MenuSelect or MenuKey, since these are the only times that
  547.     a menu item can be selected. Note that MenuSelect is also the only time
  548.     the user will see menu items. This approach to deciding what enable/
  549.     disable state a menu item has the advantage of concentrating all
  550.     the decision-making in one routine, as opposed to being spread throughout
  551.     the application. Other application designs may take a different approach
  552.     that is just as valid. */
  553.  
  554. #pragma segment Main
  555. void AdjustMenus()
  556. {
  557.     WindowPtr    window;
  558.     MenuHandle    menu;
  559.  
  560.     window = FrontWindow();
  561.  
  562.     menu = GetMHandle(mFile);
  563.     if ( IsDAWindow(window) )        /* we can allow desk accessories to be closed from the menu */
  564.         EnableItem(menu, iClose);
  565.     else
  566.         DisableItem(menu, iClose);    /* but not our traffic light window */
  567.  
  568.     menu = GetMHandle(mEdit);
  569.     if ( IsDAWindow(window) ) {        /* a desk accessory might need the edit menu… */
  570.         EnableItem(menu, iUndo);
  571.         EnableItem(menu, iCut);
  572.         EnableItem(menu, iCopy);
  573.         EnableItem(menu, iClear);
  574.         EnableItem(menu, iPaste);
  575.     } else {                        /* …but we don’t use it */
  576.         DisableItem(menu, iUndo);
  577.         DisableItem(menu, iCut);
  578.         DisableItem(menu, iCopy);
  579.         DisableItem(menu, iClear);
  580.         DisableItem(menu, iPaste);
  581.     }
  582.     menu = GetMHandle(mIPOptions);
  583.     if ( ! IsDAWindow(window) ) {    /* if not a desk accessory window */
  584.         EnableItem(menu, iRecordRoute);
  585.         DisableItem(menu, iLooseSourceRR);
  586.         DisableItem(menu, iStrictSourceRR);
  587.         DisableItem(menu, iTimeStamp);
  588.         DisableItem(menu, iSecurity);
  589.         DisableItem(menu, iStreamID);
  590.     }
  591. } /*AdjustMenus*/
  592.  
  593.  
  594. /*    This is called when an item is chosen from the menu bar (after calling
  595.     MenuSelect or MenuKey). It performs the right operation for each command.
  596.     It is good to have both the result of MenuSelect and MenuKey go to
  597.     one routine like this to keep everything organized. */
  598.  
  599. #pragma segment Main
  600. void DoMenuCommand(menuResult)
  601.     long        menuResult;
  602. {
  603.     short        menuID;                /* the resource ID of the selected menu */
  604.     short        menuItem;            /* the item number of the selected menu */
  605.     short        itemHit;
  606.     Str255        daName;
  607.     short        daRefNum;
  608.     Boolean        handledByDA;
  609.  
  610.     menuID = HiWord(menuResult);    /* use macros for efficiency to... */
  611.     menuItem = LoWord(menuResult);    /* get menu item number and menu number */
  612.     switch ( menuID ) {
  613.         case mApple:
  614.             switch ( menuItem ) {
  615.                 case iAbout:        /* bring up alert for About */
  616.                     itemHit = Alert(rAboutAlert, nil);
  617.                     break;
  618.                 default:            /* all non-About items in this menu are DAs */
  619.                     /* type Str255 is an array in MPW 3 */
  620.                     GetItem(GetMHandle(mApple), menuItem, daName);
  621.                     daRefNum = OpenDeskAcc(daName);
  622.                     break;
  623.             }
  624.             break;
  625.         case mFile:
  626.             switch ( menuItem ) {
  627.                 case iClose:
  628.                     DoCloseWindow(FrontWindow());
  629.                     break;
  630.                 case iQuit:
  631.                     Terminate();
  632.                     break;
  633.             }
  634.             break;
  635.         case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  636.             handledByDA = SystemEdit(menuItem-1);    /* since we don’t do any Editing */
  637.             break;
  638.         case mIPOptions:
  639.             if (!gRunning) {            /* we won't allow the user to change the */
  640.                 switch ( menuItem ) {    /* options while we are sending icmp echoes */
  641.                     case iRecordRoute:
  642.                         gRR = !gRR;
  643.                         CheckItem(GetMHandle(mIPOptions), iRecordRoute, gRR);
  644.                         break;
  645.                     case iLooseSourceRR:
  646.                         gLSRR = !gLSRR;
  647.                         break;
  648.                     case iStrictSourceRR:
  649.                         gSSRR = !gSSRR;
  650.                         break;
  651.                     case iTimeStamp:
  652.                         gTimestamp = !gTimestamp;
  653.                         break;
  654.                     case iSecurity:
  655.                         gSecurity = !gSecurity;
  656.                         break;
  657.                     case iStreamID:
  658.                         gSID = !gSID;
  659.                         break;
  660.                 }
  661.             }
  662.             break;
  663.     }
  664.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  665. } /*DoMenuCommand*/
  666.  
  667.  
  668. /* Close a window. This handles desk accessory and application windows. */
  669.  
  670. /*    1.01 - At this point, if there was a document associated with a
  671.     window, you could do any document saving processing if it is 'dirty'.
  672.     DoCloseWindow would return true if the window actually closed, i.e.,
  673.     the user didn’t cancel from a save dialog. This result is handy when
  674.     the user quits an application, but then cancels the save of a document
  675.     associated with a window. */
  676.  
  677. #pragma segment Main
  678. Boolean DoCloseWindow(window)
  679.     WindowPtr    window;
  680. {
  681.     if ( IsDAWindow(window) )
  682.         CloseDeskAcc(((WindowPeek) window)->windowKind);
  683.     else if ( IsAppWindow(window) )
  684.         CloseWindow(window);
  685.     return true;
  686. } /*DoCloseWindow*/
  687.  
  688.  
  689. /* Clean up the application and exit. We close all of the windows so that
  690.  they can update their documents, if any. */
  691.  
  692. /*    1.01 - If we find out that a cancel has occurred, we won't exit to the
  693.     shell, but will return instead. */
  694.  
  695. #pragma segment Main
  696. void Terminate()
  697. {
  698.     WindowPtr    aWindow;
  699.     Boolean        closed;
  700.     OSErr        err;
  701.  
  702.     closed = true;
  703.     do {
  704.         aWindow = FrontWindow();                /* get the current front window */
  705.         if (aWindow != nil)
  706.             closed = DoCloseWindow(aWindow);    /* close this window */    
  707.     }
  708.     while (closed && (aWindow != nil));
  709.     err = CloseResolver();
  710.     if (closed)
  711.         ExitToShell();                            /* exit if no cancellation */
  712. } /*Terminate*/
  713.  
  714.  
  715. /*    Set up the whole world, including global variables, Toolbox managers,
  716.     and menus. We also create our one application window at this time.
  717.     Since window storage is non-relocateable, how and when to allocate space
  718.     for windows is very important so that heap fragmentation does not occur.
  719.     Because Sample has only one window and it is only disposed when the application
  720.     quits, we will allocate its space here, before anything that might be a locked
  721.     relocatable object gets into the heap. This way, we can force the storage to be
  722.     in the lowest memory available in the heap. Window storage can differ widely
  723.     amongst applications depending on how many windows are created and disposed. */
  724.  
  725. /*    1.01 - The code that used to be part of ForceEnvirons has been moved into
  726.     this module. If an error is detected, instead of merely doing an ExitToShell,
  727.     which leaves the user without much to go on, we call AlertUser, which puts
  728.     up a simple alert that just says an error occurred and then calls ExitToShell.
  729.     Since there is no other cleanup needed at this point if an error is detected,
  730.     this form of error- handling is acceptable. If more sophisticated error recovery
  731.     is needed, an exception mechanism, such as is provided by Signals, can be used. */
  732.  
  733. #pragma segment Initialize
  734. void Initialize()
  735. {
  736.     Handle        menuBar;
  737.     DialogPtr    ldialog;
  738.     long        total, contig;
  739.     EventRecord event;
  740.     short        count;
  741.     OSErr        err;
  742.     Ptr            storage;
  743.     Rect        destRect, viewRect;
  744.     DocumentPeek    doc;
  745.  
  746.     gInBackground = false;
  747.  
  748.     InitGraf((Ptr) &qd.thePort);
  749.     InitFonts();
  750.     InitWindows();
  751.     InitMenus();
  752.     TEInit();
  753.     InitDialogs(nil);
  754.     InitCursor();
  755.     InitCursorCtl(nil);
  756.     
  757.     /*    Call MPPOpen and ATPLoad at this point to initialize AppleTalk,
  758.          if you are using it. */
  759.     /*    NOTE -- It is no longer necessary, and actually unhealthy, to check
  760.         PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  761.         of checking for port availability themselves. */
  762.     
  763.     /*    This next bit of code is necessary to allow the default button of our
  764.         alert be outlined.
  765.         1.02 - Changed to call EventAvail so that we don't lose some important
  766.         events. */
  767.      
  768.     for (count = 1; count <= 3; count++)
  769.         EventAvail(everyEvent, &event);
  770.     
  771.     /*    Ignore the error returned from SysEnvirons; even if an error occurred,
  772.         the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  773.         call to SysEnvirons by calling it after initializing AppleTalk. */
  774.      
  775.     SysEnvirons(kSysEnvironsVersion, &gMac);
  776.     
  777.     /* Make sure that the machine has at least 128K ROMs. If it doesn't, exit. */
  778.     
  779.     if (gMac.machineType < 0) AlertUser(sWrongMachine);
  780.     
  781.     /*    1.02 - Move TrapAvailable call to after SysEnvirons so that we can tell
  782.         in TrapAvailable if a tool trap value is out of range. */
  783.         
  784.     gHasWaitNextEvent = TrapAvailable(_WaitNextEvent, ToolTrap);
  785.  
  786.     /*    1.01 - We used to make a check for memory at this point by examining ApplLimit,
  787.         ApplicZone, and StackSpace and comparing that to the minimum size we told
  788.         MultiFinder we needed. This did not work well because it assumed too much about
  789.         the relationship between what we asked MultiFinder for and what we would actually
  790.         get back, as well as how to measure it. Instead, we will use an alternate
  791.         method comprised of two steps. */
  792.      
  793.     /*    It is better to first check the size of the application heap against a value
  794.         that you have determined is the smallest heap the application can reasonably
  795.         work in. This number should be derived by examining the size of the heap that
  796.         is actually provided by MultiFinder when the minimum size requested is used.
  797.         The derivation of the minimum size requested from MultiFinder is described
  798.         in Sample.h. The check should be made because the preferred size can end up
  799.         being set smaller than the minimum size by the user. This extra check acts to
  800.         insure that your application is starting from a solid memory foundation. */
  801.      
  802.     if ((long) GetApplLimit() - (long) ApplicZone() < kMinHeap)
  803.         AlertUser(sAppMemSmall);
  804.     
  805.     /*    Next, make sure that enough memory is free for your application to run. It
  806.         is possible for a situation to arise where the heap may have been of required
  807.         size, but a large scrap was loaded which left too little memory. To check for
  808.         this, call PurgeSpace and compare the result with a value that you have determined
  809.         is the minimum amount of free memory your application needs at initialization.
  810.         This number can be derived several different ways. One way that is fairly
  811.         straightforward is to run the application in the minimum size configuration
  812.         as described previously. Call PurgeSpace at initialization and examine the value
  813.         returned. However, you should make sure that this result is not being modified
  814.         by the scrap's presence. You can do that by calling ZeroScrap before calling
  815.         PurgeSpace. Make sure to remove that call before shipping, though. */
  816.     
  817.     /* ZeroScrap(); */
  818.  
  819.     PurgeSpace(&total, &contig);
  820.     if (total < kMinSpace) AlertUser(sAppMemSmall);
  821.  
  822.     /*    The extra benefit to waiting until after the Toolbox Managers have been initialized
  823.         to check memory is that we can now give the user an alert to tell him/her what
  824.         happened. Although it is possible that the memory situation could be worsened by
  825.         displaying an alert, MultiFinder would gracefully exit the application with
  826.         an informative alert if memory became critical. Here we are acting more
  827.         in a preventative manner to avoid future disaster from low-memory problems. */
  828.  
  829.     gRunning = false;    /* initialize the icmp echo sending flag */
  830.     /*     we will allocate our own window storage instead of letting the Window
  831.         Manager do it because GetNewWindow may load in temp. resources before
  832.         making the NewPtr call, and this can lead to heap fragmentation. */
  833.     storage = NewPtr(sizeof(DocumentRecord));
  834.     if ( storage == nil ) AlertUser(sAppMemSmall);
  835.     if ( !GoGetRect(rMsgRect, &gMsgRect) )
  836.         AlertUser(sNoRectRes);                        /* the ICMP message rectangle */
  837.     ldialog = GetNewDialog(rDialog, storage, (WindowPtr) -1);
  838.  
  839.     ShowWindow(ldialog);
  840.     SetPort(ldialog);
  841.     DoInitDialog(ldialog);
  842.     TextMode(srcCopy);
  843.     TextFont(monaco);
  844.     TextSize(9);
  845.     doc = (DocumentPeek) ldialog;
  846.     GetTERect(ldialog, &viewRect);
  847.     destRect = viewRect;
  848.     doc->docTE = TENew(&destRect, &viewRect);
  849.     if (doc->docTE == nil) AlertUser(sNoTERes);
  850.     AdjustViewRect(doc->docTE);
  851.     TEAutoView(true, doc->docTE);
  852.     doc->docVScroll = GetNewControl(rVScroll, (WindowPtr) doc);
  853.     if (doc->docVScroll == nil) AlertUser(sNoVScrollBar);
  854.     AdjustScrollValues(ldialog, false);
  855.     ShowWindow(ldialog);
  856.  
  857.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  858.     if ( menuBar == nil ) AlertUser(sNoMenuBar);
  859.     SetMenuBar(menuBar);                    /* install menus */
  860.     DisposHandle(menuBar);
  861.     AddResMenu(GetMHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  862.     DrawMenuBar();
  863.  
  864.     err = OpenResolver(nil);
  865.     if (err != noErr) {
  866.         SysBeep(5); SysBeep(10); SysBeep(55);
  867.         AlertUser(sResolverErr);            /* fatal error */
  868.     }
  869. } /*Initialize*/
  870.  
  871.  
  872. /*    This utility loads the global rectangles that are used by the window
  873.     drawing routines. It shows how the resource manager can be used to hold
  874.     values in a convenient manner. These values are then easily altered without
  875.     having to re-compile the source code. In this particular case, we know
  876.     that this routine is being called at initialization time. Therefore,
  877.     if a failure occurs here, we will assume that the application is in such
  878.     bad shape that we should just exit. Your error handling may differ, but
  879.     the check should still be made. */
  880.     
  881. #pragma segment Initialize
  882. Boolean GoGetRect(rectID,theRect)
  883.     short    rectID;
  884.     Rect    *theRect;
  885. {
  886.     Handle        resource;
  887.     
  888.     resource = GetResource('RECT', rectID);
  889.     if ( resource != nil ) {
  890.         *theRect = **((Rect**) resource);
  891.         return true;
  892.     }
  893.     else
  894.         return false;
  895. } /* GoGetRect */
  896.  
  897.  
  898. /*    Check to see if a window belongs to the application. If the window pointer
  899.     passed was NIL, then it could not be an application window. WindowKinds
  900.     that are negative belong to the system and windowKinds less than userKind
  901.     are reserved by Apple except for windowKinds equal to dialogKind, which
  902.     mean it is a dialog.
  903.     1.02 - In order to reduce the chance of accidentally treating some window
  904.     as an AppWindow that shouldn't be, we'll only return true if the windowkind
  905.     is userKind. If you add different kinds of windows to Sample you'll need
  906.     to change how this all works. */
  907.  
  908. #pragma segment Main
  909. Boolean IsAppWindow(window)
  910.     WindowPtr    window;
  911. {
  912.     short        windowKind;
  913.  
  914.     if ( window == nil )
  915.         return false;
  916.     else {    /* application windows have windowKinds = userKind (8) */
  917.         windowKind = ((WindowPeek) window)->windowKind;
  918.         return (windowKind = userKind);
  919.     }
  920. } /*IsAppWindow*/
  921.  
  922.  
  923. /* Check to see if a window belongs to a desk accessory. */
  924.  
  925. #pragma segment Main
  926. Boolean IsDAWindow(window)
  927.     WindowPtr    window;
  928. {
  929.     if ( window == nil )
  930.         return false;
  931.     else    /* DA windows have negative windowKinds */
  932.         return ((WindowPeek) window)->windowKind < 0;
  933. } /*IsDAWindow*/
  934.  
  935.  
  936. /*    Check to see if a given trap is implemented. This is only used by the
  937.     Initialize routine in this program, so we put it in the Initialize segment.
  938.     The recommended approach to see if a trap is implemented is to see if
  939.     the address of the trap routine is the same as the address of the
  940.     Unimplemented trap. */
  941. /*    1.02 - Needs to be called after call to SysEnvirons so that it can check
  942.     if a ToolTrap is out of range of a pre-MacII ROM. */
  943.  
  944. #pragma segment Initialize
  945. Boolean TrapAvailable(tNumber,tType)
  946.     short        tNumber;
  947.     TrapType    tType;
  948. {
  949.     if ( ( tType == ToolTrap ) &&
  950.         ( gMac.machineType > envMachUnknown ) &&
  951.         ( gMac.machineType < envMacII ) ) {        /* it's a 512KE, Plus, or SE */
  952.         tNumber = tNumber & 0x03FF;
  953.         if ( tNumber > 0x01FF )                    /* which means the tool traps */
  954.             tNumber = _Unimplemented;            /* only go to 0x01FF */
  955.     }
  956.     return NGetTrapAddress(tNumber, tType) != GetTrapAddress(_Unimplemented);
  957. } /*TrapAvailable*/
  958.  
  959.  
  960. /*    Display an alert that tells the user an error occurred, then exit the program.
  961.     This routine is used as an ultimate bail-out for serious errors that prohibit
  962.     the continuation of the application. Errors that do not require the termination
  963.     of the application should be handled in a different manner. Error checking and
  964.     reporting has a place even in the simplest application. The error number is used
  965.     to index an 'STR#' resource so that a relevant message can be displayed. */
  966.  
  967. #pragma segment Main
  968. void AlertUser(short error)
  969. short error;
  970. {
  971.     short        itemHit;
  972.     Str255        message;
  973.  
  974.     SetCursor(&qd.arrow);
  975.     GetIndString(message, rErrorStrings, error);
  976.     ParamText(message, "", "", "");
  977.     itemHit = Alert(rUserAlert, nil);
  978.     ExitToShell();
  979. } /* AlertUser */
  980.  
  981. #pragma segment Main
  982. void WarnUser(short error)
  983. short error;
  984. {
  985.     short        itemHit;
  986.     Str255        message;
  987.  
  988.     SetCursor(&qd.arrow);
  989.     GetIndString(message, rErrorStrings, error);
  990.     ParamText(message, "", "", "");
  991.     itemHit = Alert(rUserAlert, nil);
  992. } /* WarnUser */
  993.  
  994. #pragma segment Main
  995. void DrawPingStatistics(window)
  996. WindowPtr window;
  997. {
  998.     Rect        tRect;
  999.     TEHandle    te;
  1000.  
  1001.     FrameRect(&gMsgRect);
  1002.  
  1003.     te = ((DocumentPeek) window)->docTE;
  1004.     tRect = (**te).viewRect;
  1005.  
  1006.     EraseRect(&tRect);
  1007.     TEUpdate(&tRect, te);
  1008. }
  1009.  
  1010. #pragma segment Main
  1011. void DrawOnScreen(void)
  1012. {
  1013.     WindowPtr window;
  1014.     TEHandle te;
  1015.     char ourString1[255], ourString2[255], tIPAddrStr[5];
  1016.     unsigned long *tIPAddrLPtr;
  1017.     Rect teRect;
  1018.     short tindex, totlength;
  1019.  
  1020.     if (window = FrontWindow())
  1021.         if ( IsAppWindow(window) ) {
  1022.             SetPort(window);
  1023.             BeginUpdate(window);
  1024.             te = ((DocumentPeek) window)->docTE;
  1025.             teRect = (**te).viewRect;
  1026.  
  1027.             getindstring(ourString1, rErrorStrings, sPktInfo);
  1028.             sprintf(ourString2, "%d/%d/%d/%d\n", gIcmpStat.totPktOut,
  1029.                 gIcmpStat.totPktIn, gIcmpStat.totPktBad, gIcmpStat.pktLoss);
  1030.             strcat(ourString1, ourString2);
  1031.             TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1032.             getindstring(ourString1, rErrorStrings, sTimeInfo);
  1033.             sprintf(ourString2, "%u/%u/%u\n",
  1034.                 gIcmpStat.minTime, gIcmpStat.avgTime, gIcmpStat.maxTime);
  1035.             strcat(ourString1, ourString2);
  1036.             TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1037.  
  1038.             if (gRR) {
  1039.                 if (gIPOptionResponse[0] != '\0') {
  1040.                     getindstring(ourString1, rErrorStrings, sRecordedRoute);
  1041.                     TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1042.                     tIPAddrLPtr = (unsigned long *) tIPAddrStr;
  1043.                     tindex = 3;    /* format of the Record Route option blk */
  1044.                     totlength = gIPOptionResponse[2] - 1;
  1045.                     while((tindex < totlength) && (tindex < MAX_OPT_SIZE)) {
  1046.                         mycopy(tIPAddrStr, &gIPOptionResponse[tindex], 4);
  1047.                         AddrToStr(*tIPAddrLPtr, ourString1);
  1048.                         strcat(ourString1, "\n");
  1049.                         TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1050.                         tindex += 4;
  1051.                     }
  1052.                 }
  1053.                 else {
  1054.                     getindstring(ourString1, rErrorStrings, sNoRoutesRecorded);
  1055.                     TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1056.                 }
  1057.             }
  1058.             getindstring(ourString1, rErrorStrings, sPingTail);
  1059.             TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1060.             GetTERect(window, &teRect);
  1061.             teRect.right += kScrollbarWidth;    /* for scroll bar */
  1062.             InvalRect(&teRect);
  1063.             TEUpdate(&teRect, te);
  1064.             AdjustScrollbars(window, false);
  1065.             AdjustTE(window);
  1066.             EndUpdate(window);
  1067.         }
  1068. }
  1069.  
  1070. #pragma segment Main
  1071. void DrawPacketOnScreen(void)
  1072. {
  1073.     WindowPtr window;
  1074.     TEHandle te;
  1075.     char ourString1[255], ourString2[255];
  1076.     Rect teRect;
  1077.  
  1078.     if (gResponseStatus.pkt_no != 0)
  1079.         if (window = FrontWindow())
  1080.             if ( IsAppWindow(window) ) {
  1081.                 SetPort(window);
  1082.                 BeginUpdate(window);
  1083.                 te = ((DocumentPeek) window)->docTE;
  1084.                 teRect = (**te).viewRect;
  1085.     
  1086.                 if (gResponseStatus.response_ok == true) {
  1087.                     getindstring(ourString1, rErrorStrings, sSuccessPktStatus);
  1088.                     sprintf(ourString2, "%d/%d/%d\n", gResponseStatus.pkt_no,
  1089.                         gResponseStatus.rt_time, gResponseStatus.pkt_len);
  1090.                 }
  1091.                 else {
  1092.                     getindstring(ourString1, rErrorStrings, sTimeoutPktStatus);
  1093.                     sprintf(ourString2, "%d\n", gResponseStatus.pkt_no);
  1094.                 }
  1095.                 strcat(ourString1, ourString2);
  1096.                 TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1097.     
  1098.                 GetTERect(window, &teRect);
  1099.                 teRect.right += kScrollbarWidth;    /* for scroll bar */
  1100.                 InvalRect(&teRect);
  1101.                 TEUpdate(&teRect, te);
  1102.                 AdjustScrollbars(window, false);
  1103.                 AdjustTE(window);
  1104.                 EndUpdate(window);
  1105.                 gResponseStatus.pkt_no = 0;
  1106.                 gResponseStatus.rt_time = 0;
  1107.                 gResponseStatus.pkt_len = 0;
  1108.                 gResponseStatus.response_ok = true;
  1109.             }
  1110. }
  1111.  
  1112. #pragma segment Main
  1113. void DrawErrorMessageOnScreen(errMsg)
  1114. short errMsg;
  1115. {
  1116.     WindowPtr window;
  1117.     TEHandle te;
  1118.     char ourString1[255];
  1119.     Rect teRect;
  1120.  
  1121.     if (window = FrontWindow())
  1122.         if ( IsAppWindow(window) ) {
  1123.             SetPort(window);
  1124.             BeginUpdate(window);
  1125.             te = ((DocumentPeek) window)->docTE;
  1126.             teRect = (**te).viewRect;
  1127.  
  1128.             getindstring(ourString1, rErrorStrings, errMsg);
  1129.             TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1130.  
  1131.             GetTERect(window, &teRect);
  1132.             teRect.right += kScrollbarWidth;    /* for scroll bar */
  1133.             InvalRect(&teRect);
  1134.             TEUpdate(&teRect, te);
  1135.             AdjustScrollbars(window, false);
  1136.             AdjustTE(window);
  1137.             EndUpdate(window);
  1138.         }
  1139. }
  1140.  
  1141. #pragma segment Main
  1142. void DrawHostInfoOnScreen(void)
  1143. {
  1144.     WindowPtr window;
  1145.     TEHandle te;
  1146.     char ourString1[255];
  1147.     char ourString2[255];
  1148.     Rect teRect;
  1149.  
  1150.     if (window = FrontWindow()) {
  1151.         if ( IsAppWindow(window) ) {
  1152.             SetPort(window);
  1153.             BeginUpdate(window);
  1154.             te = ((DocumentPeek) window)->docTE;
  1155.             teRect = (**te).viewRect;
  1156.  
  1157.             getindstring(ourString1, rErrorStrings, sSourceAddress);
  1158.             strcat(ourString1, "[");
  1159.             strcat(ourString1, gSourceAddress);
  1160.             strcat(ourString1, "] --> ");
  1161.  
  1162.             getindstring(ourString2, rErrorStrings, sDestinationAddress);
  1163.             strcat(ourString1, ourString2);
  1164.  
  1165.             if ((gHostAddress[0] < '0') || (gHostAddress[0] > '9')) {
  1166.                 strcat(ourString1, gHostAddress);
  1167.                 strcat(ourString1, " ");
  1168.             }
  1169.             strcat(ourString1, "[");
  1170.             strcat(ourString1, gDestAddress);
  1171.             strcat(ourString1, "]\n");
  1172.  
  1173.             TEInsert((Ptr) ourString1, strlen(ourString1), te);
  1174.  
  1175.             GetTERect(window, &teRect);
  1176.             teRect.right += kScrollbarWidth;
  1177.             InvalRect(&teRect);
  1178.             TEUpdate(&teRect, te);
  1179.             AdjustScrollbars(window, false);
  1180.             AdjustTE(window);
  1181.             EndUpdate(window);
  1182.         }
  1183.     }
  1184. }
  1185.